<?php

	class classes {
		

		function classes() {
			global $admin;

			//if(empty($_SESSION["managerid"])) $admin->login();
		}
		
		function index() {
			global $db, $smarty, $config, $segment, $admin;
			//if(!$admin->rights("classes_view")) return;
			
			if(!empty($_POST["ordersn"]))
				foreach($_POST["ordersn"] as $k => $v)
					$db->execute("update {$config->db_prefix}classes set ordersn=$v where id=$k");
		
			if(!empty($segment[1])) $smarty->assign("paths", $this->_getpaths($segment[1]));
		}


		function create() {
			global $db, $smarty, $config, $segment, $admin;
			//if(!$admin->rights("classes_create")) return;
			
			if(!empty($_POST["action"]) && $_POST["action"]=="create") {
				if(empty($_POST["title"])) $errors["empty_title"] = true;
// 				if(empty($_POST["priceunderline"])) $errors["empty_priceunderline"] = true;
				
// 				if(empty($_POST["priceline"])) $errors["empty_priceline"] = true;
				

				if(empty($errors)) {
					$title = $db->qstr($_POST["title"]);
					$ordersn = $db->qstr($_POST["ordersn"]);
					
					$priceunderline = $db->qstr($_POST["priceunderline"]);
					$priceline = $db->qstr($_POST["priceline"]);
					$warningnum = $db->qstr($_POST["warningnum"]);
					$canbuyout = $db->qstr($_POST["canbuyout"]);
					$sequence = 0;
					$sqlstr = "insert into {$config->db_prefix}classes (title, ordersn, sequence,priceunderline,priceline,warningnum,canbuyout) values ($title, $ordersn, $sequence,$priceunderline,$priceline,$warningnum,$canbuyout)";
					$result = $db->execute($sqlstr);
					$id = $db->insert_id();
					$parentid = 0;
					$path = ",0,";
					if(!empty($_POST["parentid"])) {
						$parentid = $_POST["parentid"];
						$path = $db->getone("select path from {$config->db_prefix}classes where id=$parentid");
					}
					$path = $db->qstr("$path$id,");
					$result = $db->execute("update {$config->db_prefix}classes set parentid=$parentid, path=$path where id=$id");
					if($result) header("location:{$config->site_admin}m=inventory&s=classes.php");
				}
								
				if(!empty($errors)) $smarty->assign("errors", $errors);								
			}
			
			$smarty->assign("myclasses",$this->_gettree());
			$smarty->assign("myclassesid",$_GET['id']);
			$smarty->assign("myparentid",$this->parentid);
				
							

			if(!empty($segment[1])) $smarty->assign("paths", $this->_getpaths($segment[1]));
		}
		
		function modify() {
			global $db, $smarty, $config, $segment, $admin;
			//if(!$admin->rights("classes_modify")) return;
			
			if(!empty($_POST["action"]) && $_POST["action"]=="modify") {
				if(empty($_POST["title"])) $errors["empty_title"] = true;
// 				if(empty($_POST["priceunderline"])) $errors["empty_priceunderline"] = true;
				
// 				if(empty($_POST["priceline"])) $errors["empty_priceline"] = true;

				if(empty($errors)) {
					$id = $db->qstr($_POST["id"]);
					$title = $db->qstr($_POST["title"]);
					$ordersn = $db->qstr($_POST["ordersn"]);
					$priceunderline = $db->qstr($_POST["priceunderline"]);
					$priceline = $db->qstr($_POST["priceline"]);
					$warningnum = $db->qstr($_POST["warningnum"]);
					$canbuyout = $db->qstr($_POST["canbuyout"]);
					
					$sqlstr = "update {$config->db_prefix}classes set title=$title, ordersn=$ordersn,priceunderline=$priceunderline,priceline=$priceline,warningnum=$warningnum,canbuyout=$canbuyout where id=$id";
					$result = $db->execute($sqlstr);
					if($result) header("location:".(empty($_POST["referer"])?"{$config->site_index}admin/classes":$_POST["referer"]));
				}
								
				if(!empty($errors)) $smarty->assign("errors", $errors);								
			}
			$smarty->assign("myclasses",$this->_gettree());
			$smarty->assign("myclassesid",$_GET['id']);
			$smarty->assign("myparentid",$this->parentid);

			if(!empty($segment[1])) $smarty->assign("paths", $this->_getpaths($segment[1]));
		}


		function remove() {
			global $db, $segment, $config, $admin;
			//if(!$admin->rights("classes_remove")) return;
			$sqlstr = "delete from {$config->db_prefix}classes where path like ".$db->qstr("%,$_GET[id],%");
			$result = $db->execute($sqlstr);
			if($result) header("location:".(empty($_SERVER['HTTP_REFERER'])?"{$config->site_index}admin/classes":$_SERVER['HTTP_REFERER']));
		}


		
		function recaches() {
			global $db, $config;

			if(!empty($_POST["action"])){
				$i = 0;
				$rssorts = $this->jssorts(0);
				foreach($rssorts as $k => $v) {
					$result = $db->execute("update {$config->db_prefix}classes set sequence = " . (++$i) . " where id={$v['id']}");
				}
				if($result) header("location:".(empty($_POST["referer"])?"{$config->site_index}admin/classes":$_POST["referer"]));
			}
		}

		function jssorts($classid) {
			global $db, $rssorts, $config;
							
			$rs = $db->getall("select * from {$config->db_prefix}classes where parentid={$classid} order by ordersn");
			if($rs)
				foreach($rs as $k => $v) {
					$rssorts[] = $v;
					$this->jssorts($v["id"]);
				}
			
			return $rssorts;
		}



		
		
		private function _getpaths($id) {
			global $db, $config;

			$paths = array();
			$path = $db->getone("select path from {$config->db_prefix}classes where id=$id");
			$arr_path = explode(",", $path);
			if(!empty($path)) foreach($arr_path as $k => $v)
			if($v) $paths[] = $db->getrow("select * from {$config->db_prefix}classes where id=$v");
			return $paths;
		}
		
		private function _gettree($id=0){
		    global $db, $config;
		    $tree =array();
		    $tree_info=$db->GetArray("select * from {$config->db_prefix}classes where parentid=$id order by ordersn");
		    if (empty($tree_info))return null;
		    //var_dump($tree_info);
		    foreach ($tree_info as $value) {
		        $tree[$value['id']]=$value;
		        if($children=$this->_gettree($value['id'])){
		            $tree[$value['id']]['children']=$children;
		        }
		        if ($value['id']==$_GET['id']){
		            $this->parentid=$value['parentid'];
		        }
		    }
		    return $tree;
		}
		
		function api($id=0){
		    echo json_encode($this->_gettree($id));
		    exit;
		}

}
?>